package org.jypj.dev.controller.back;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.jypj.dev.controller.BaseController;
import org.jypj.dev.dao.ShoesDao;
import org.jypj.dev.model.Shoes;
import org.jypj.dev.service.ShoesService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.FileUtil;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.vo.PageVo;
import org.jypj.dev.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author ChenYu
 * @date 2017-06-12
 */
@Controller
@RequestMapping("back/shoes")
public class BackShoesController extends BaseController {

    @Resource
    private ShoesService shoesService;

    @Resource
    private ShoesDao shoesDao;

    /**
     * 添加鞋子
     *
     * @return
     */
    @RequestMapping(value = "save")
    public String toShoesSave() {

        return "back/shoes_form";
    }

    /**
     * 添加鞋子
     *
     * @return
     */
    @RequestMapping(value = "toShoesEdit/{id}")
    public String toShoesEdit(@PathVariable String id, Model model) {
        Shoes shoes = shoesService.selectShoesById(id);
        String join = StringUtil.getShoesImgStr(shoes.getShoesImgList());
        model.addAttribute("shoes", shoes);
        model.addAttribute("otherImg", join);
        return "back/shoes_form";
    }

    /**
     * shoeList
     *
     * @return
     */
    @RequestMapping(value = "all")
    public String toShoesList() {

        return "back/shoes_list";
    }

    /**
     * 查询所有鞋子
     *
     * @param shoes
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAll")
    public Result selectShoes(Shoes shoes, PageVo pageVo) {
        Result result = new Result();
        try {
            Page<Shoes> page = shoesService.selectOnePageByShoes(pageVo, shoes);
            result.getDataMap().put("totalRows", page.getTotal());
            result.getDataMap().put("result", page.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg("网络异常");
            result.setExcMsg(e.toString());
        }
        return result;
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "uploadFile")
    public Result uploadFile(MultipartFile file) {
        Result result = new Result();
        try {
            String upload = FileUtil.upload("/shoesImg", file);
            result.getDataMap().put("imgPath", upload);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
        }
        return result;
    }


    /**
     * 保存或者更新
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveOrUpdate")
    public Result saveShoes(String data) {
        Result result = new Result();
        System.out.println(data);
        try {
            Shoes shoes = JSON.parseObject(data, Shoes.class);
            String todayString = DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss");
            shoes.setModifyTime(todayString);
            if (StringUtil.isEmpty(shoes.getId())) {
                shoes.setCreateTime(todayString);
                shoesService.saveShoes(shoes);
            } else {
                shoesService.updateShoes(shoes);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
        }
        return result;
    }

    /**
     * 保存或者更新
     *
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateShoesStatus")
    public Result updateShoes(String id, String status) {
        Result result = new Result();
        try {
            shoesDao.updateShoesStatus(status, id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg("网络异常");
            result.setExcMsg(e.toString());
        }
        return result;
    }


    /**
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete/{id}")
    public Result deleteShoes(@PathVariable String id) {
        Result result = new Result();
        try {
            shoesService.deleteShoesById(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg("网络异常");
            result.setExcMsg(e.toString());
        }
        return result;
    }
}
